package com.minelx.table.core.series.index;

import com.minelx.table.core.series.Series;

import java.util.stream.LongStream;

public class BoolSeriesIndexes implements Indexes {
	private final Indexes decorated;

	private final Series<Boolean> booleanIndexes;

	private final long len;

	private BoolSeriesIndexes(Indexes decorated, Series<Boolean> booleanIndexes) {
		this.decorated = decorated;
		this.booleanIndexes = booleanIndexes;
		len = booleanIndexes.stream().filter(Boolean.TRUE::equals).count();
	}

	@Override
	public LongStream stream() {
		return decorated.stream().filter(booleanIndexes::at);
	}

	@Override
	public long len() {
		return len;
	}

	@Override
	public long at(long index) {
		throw new RuntimeException("BoolSeriesIndexes.at is not implemented yet.");
	}

	public static Indexes create(Indexes decorated, Series<Boolean> booleanIndexes) {
		return Indexes.array(decorated.stream().filter(booleanIndexes::at).toArray());
	}
}
